package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.annotation.JsonCreator.Mode;

import dto.ResponseDto;
import pojo.Categories;
import pojo.Food;
import pojo.Merchant;
import service.CategoriesService;
import service.FileUploadService;
import service.FoodService;

@Controller
public class FoodController {

	@Resource(name="foodServiceImpl")
	private FoodService foodService;
	
	@Resource(name = "categoriesServiceImpl")
    private CategoriesService categoriesService;
    @Resource
    Environment env;
    @Resource(name="fileUploadService")
    private FileUploadService uploadService;
    
    
    @RequestMapping(value="/views/foods",method=RequestMethod.GET)
    public String getFoodList(HttpSession session,Model model) {
    	model.addAttribute("merchant", session.getAttribute("user"));
    	return "WEB-INF/food/foodList";
    }
    
    @RequestMapping(value="/views/foods/add",method=RequestMethod.GET)
	public String redirectToAddFood(Model model,HttpSession session){
    	model.addAttribute("merchant", session.getAttribute("user"));
		return "WEB-INF/food/addFood";
	}

	
	@ResponseBody
	@RequestMapping(value="/foods",method = RequestMethod.GET)
	public List<Food> getFoodList(HttpServletRequest request,@RequestParam("food_type_id") String typeId){
		HttpSession session = request.getSession();
		Merchant merchant = (Merchant) session.getAttribute("user");
		String mId = merchant.getId();
		List<Food> foodList = new ArrayList<Food>();
		String tId = (typeId == null ? "all" : typeId);
		foodList = foodService.findFood(tId,mId);
		return foodList;
	}
	
	@ResponseBody
	@RequestMapping(value="/foods/{id}",method = RequestMethod.DELETE)
	public Food deleteFood(@PathVariable String id){
		return foodService.delete(id);
	}
	 
	@RequestMapping(value="/foods",method = RequestMethod.POST)
	public String addFood(Food food, HttpServletRequest request,MultipartFile foodImage,String categoryId){
		String url = env.getProperty("image.server.url");
		String foodImageSavePath = env.getProperty("food.savePath");
		String[] names = foodImage.getOriginalFilename().split("\\.");
		String foodImageSuffix = names[names.length - 1];
		String foodImageKey = foodImageSavePath + UUID.randomUUID().toString() + "." + foodImageSuffix;
		food.setImage(foodImageKey);
		ResponseDto foodResponse;
		Merchant merchant = (Merchant) request.getSession().getAttribute("user");
		try {
			foodResponse = uploadService.upload(url, foodImage.getInputStream(), foodImageKey);
			if (foodResponse.getCode()!=1) {
				System.out.println(foodResponse.getMessage());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		Categories categories = categoriesService.load(categoryId);
		foodService.add(categories, merchant,food);
		return "redirect:/views/foods";
	}
	
	@RequestMapping(value="/foods/{id}",method = RequestMethod.GET)
	public String showEditFood(@PathVariable String id,Model model){
		  Food food = foodService.load(id);
		  List<Categories> categoriesList = categoriesService.findAll();
		  model.addAttribute("food",food);
		  model.addAttribute("categories", categoriesList);
		  return "WEB-INF/sec/control/EditFood";
	}
	
	@RequestMapping(value="/foods/edit",method = RequestMethod.POST)
	public String editFood(Food food, HttpServletRequest request,MultipartFile foodImage,String categoryId){
    
		String url = env.getProperty("image.server.url");
		String foodImageSavePath = env.getProperty("food.savePath");
		String[] names = foodImage.getOriginalFilename().split("\\.");
		String foodImageSuffix = names[names.length - 1];
		String foodImageKey = foodImageSavePath + UUID.randomUUID().toString() + "." + foodImageSuffix;
		food.setImage(foodImageKey);
		ResponseDto foodResponse;
		Merchant merchant = (Merchant) request.getSession().getAttribute("user");
		try {
			foodResponse = uploadService.upload(url, foodImage.getInputStream(), foodImageKey);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Categories categories = categoriesService.load(categoryId);
		foodService.update(food);
		return "redirect:/views/foods";
	}
	
	
	
}
